﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace Xceed.Silverlight.ListBox
{
  [TemplateVisualState( GroupName = "GroupValueLevelStates", Name = "Leaf" )]
  [TemplateVisualState( GroupName = "GroupValueLevelStates", Name = "Root" )]
  [TemplateVisualState( GroupName = "GroupValueLevelStates", Name = "RootAndLeaf" )]
  [TemplateVisualState( GroupName = "GroupValueLevelStates", Name = "None" )]
  public class HierarchicalGroupValueItem : Control
  {
    #region Constructor
    public HierarchicalGroupValueItem()
    {
      this.DefaultStyleKey = typeof( HierarchicalGroupValueItem );
      this.UpdateVisualeStates( false );

      this.Loaded += new RoutedEventHandler( OnLoaded );
      this.Unloaded += new RoutedEventHandler( OnUnLoaded );
    }
    #endregion

    #region ContentTemplate Property

    public static readonly DependencyProperty ContentTemplateProperty = DependencyProperty.Register(
      "ContentTemplate",
      typeof( DataTemplate ),
      typeof( HierarchicalGroupValueItem ),
      new PropertyMetadata( ( DataTemplate )null ) );

    public DataTemplate ContentTemplate
    {
      get
      {
        return ( DataTemplate )this.GetValue( HierarchicalGroupValueItem.ContentTemplateProperty );
      }
      set
      {
        this.SetValue( HierarchicalGroupValueItem.ContentTemplateProperty, value );
      }
    }

    #endregion

    #region ParentHierarchicalGroupValueControl Property

    public HierarchicalGroupValueControl ParentHierarchicalGroupValueControl
    {
      get
      {
        return HierarchicalGroupValueControl.GetParentHierarchicalGroupValueControl( this );
      }
    }

    #endregion


    #region IsLeaf Property
    public bool IsLeaf
    {
      get
      {
        return m_isleaf;
      }
      internal set
      {
        if( value != m_isleaf )
        {
          m_isleaf = value;
          this.UpdateVisualeStates( true );
        }
      }
    }    
    #endregion

    #region IsRoot Property
    public bool IsRoot
    {
      get
      {
        return m_isRoot;
      }
      internal set
      {
        if( value != m_isRoot )
        {
          m_isRoot = value;
          this.UpdateVisualeStates( true );
        }
      }
    }
    #endregion

    #region Protected Methods
    public override void OnApplyTemplate()
    {
      base.OnApplyTemplate();

      this.UpdateVisualeStates( false );
    }
    #endregion

    #region private Methods
    private void OnLoaded( object sender, RoutedEventArgs e )
    {
      this.OnLoaded();
      this.UpdateVisualeStates( false );
    }

    private void OnUnLoaded( object sender, RoutedEventArgs e )
    {
      this.OnUnLoaded();
      this.UpdateVisualeStates( false );
    }

    protected virtual void OnLoaded()
    {

    }

    protected virtual void OnUnLoaded()
    {

    }

    private void UpdateVisualeStates( bool useTransition )
    {
      this.UpdateGroupValueLevelStates( useTransition );
    }


    private void UpdateGroupValueLevelStates(bool useTransition)
    {
      if( this.IsLeaf )
      {
        if( this.IsRoot )
        {
          VisualStateManager.GoToState( this, "RootAndLeaf", useTransition );
        }
        else
        {
          VisualStateManager.GoToState( this, "Leaf", useTransition );
        }
      }
      else
      {
        if( this.IsRoot )
        {
          VisualStateManager.GoToState( this, "Root", useTransition );
        }
        else
        {
          VisualStateManager.GoToState( this, "None", useTransition );
        }
      }
    }
    #endregion

    #region Private Members
    private bool m_isleaf;
    private bool m_isRoot;
    #endregion

  }
}
